package com.education.common.utils;

/**
 * 请求结果的封装
 */
public class Result {
    private Object data;
    private ResultCode resultCode;

    public Result() {

    }

    public Result(ResultCode resultCode) {
        this.resultCode = resultCode;
    }

    public Result(Object data) {
        this.resultCode = new ResultCode(ResultCode.SUCCESS, ResultCode.DEFAULT_SUCCESS_MESSAGE);
        this.data = data;
    }

    public Result(ResultCode resultCode, Object data) {
        this(resultCode);
        this.data = data;
    }

    /**
     * 响应成功
     */
    public static Result success(Object data) {
        return new Result(data);
    }

    /**
     * 响应成功
     */
    public static Result success(ResultCode resultCode, Object data) {
        return new Result(resultCode, data);
    }

    /**
     * 响应成功
     */
    public static Result success(ResultCode resultCode) {
        return new Result(resultCode);
    }

    /**
     * 响应失败
     */
    public static Result fail(ResultCode resultCode) {
        return new Result(resultCode);
    }

    /**
     * 响应失败
     */
    public static Result fail() {
        return new Result(new ResultCode(ResultCode.FAIL, ResultCode.DEFAULT_FAIL_MESSAGE));
    }

    /**
     * 请求是否成功
     */
    public boolean ifSuccess() {
        return ResultCode.SUCCESS == this.resultCode.getCode();
    }

    public <T> T getData() {
        return (T) data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResultCode getResultCode() {
        return resultCode;
    }

    public void setResultCode(ResultCode resultCode) {
        this.resultCode = resultCode;
    }
}
